package com.qyhl.webtv.module_news.news.union.special;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.glide.GlideRoundTransform;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.UnionBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by qyhl on 2017/12/1.
 */

@Route(path = ARouterPathConstant.SPECIAL)
public class SpecialListActivity extends BaseActivity implements SpecialListContract.SpecialView {

    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.listview)
    ListView listview;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.back_btn)
    ImageView backBtn;
    @BindView(R2.id.search_btn)
    TextView searchBtn;

    private SpecialListPresenter mPresenter;

    @Autowired(name = "sectionId")
    String sectionId;

    @Autowired(name = "title")
    String mTitle;

    @Autowired(name = "hasSearch")
    boolean hasSearch;

    private List<UnionBean> mList;

    private RequestOptions options;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.news_activity_union_special;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        if (hasSearch) {
            searchBtn.setVisibility(View.VISIBLE);
        } else {
            searchBtn.setVisibility(View.GONE);
        }
        mPresenter = new SpecialListPresenter(this);
        initView();

    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getDatas(sectionId);
    }

    /**
     * 初始化
     */
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        mList = new ArrayList<>();

        title.setText(StringUtils.isEmpty(mTitle) ? "专题" : mTitle);

        //glide配置
        options = new RequestOptions()
                .placeholder(R.drawable.cover_normal_default)
                .error(R.drawable.cover_normal_default)
                .priority(Priority.HIGH)
                .optionalTransform(new GlideRoundTransform(4));

        //配置刷新
        refresh.setEnableLoadMore(false);
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setDisableContentWhenRefresh(true);

        listview.setAdapter(new CommonAdapter<UnionBean>(this, R.layout.news_item_union_special, mList) {
            @Override
            protected void convert(ViewHolder viewHolder, UnionBean item, int position) {
                TextView title = viewHolder.getView(R.id.title);
                ImageView cover = viewHolder.getView(R.id.cover);

                title.setText(item.getName());
                Glide.with(SpecialListActivity.this)
                        .load(item.getLogo())
                        .apply(options)
                        .into(cover);
            }
        });

    }


    /**
     * 设置监听
     */
    private void setListeners() {
        //重载
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getDatas(sectionId);
            }
        });

        //刷新
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.getDatas(sectionId);
            }
        });

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UnionBean bean = mList.get(position);

                Bundle bundle = new Bundle();
                bundle.putString("title", bean.getName());
                bundle.putString("id", bean.getId());
                bundle.putString("cover", bean.getLogo());
                RouterManager.router(ARouterPathConstant.DETAIL_SPECIAL, bundle);
            }
        });

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build(ARouterPathConstant.SEARCH)
                        .withString("catalogId",sectionId)
                        .withInt("subCatalog",0)
                        .navigation();
            }
        });


    }

    /**
     * 设置数据
     *
     * @param beanList
     */
    @Override
    public void setDatas(List<UnionBean> beanList) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~~");
        mList.clear();
        mList.addAll(beanList);
    }

    /**
     * 出错
     *
     * @param msg
     */
    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        loadMask.setErrorText(msg);
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 无内容
     *
     * @param msg
     */
    @Override
    public void setEmpty(String msg) {
        refresh.finishRefresh();
        loadMask.setEmptyText(msg);
        loadMask.setStatus(LoadingLayout.Empty);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 无网络
     *
     * @param msg
     */
    @Override
    public void setNetError(String msg) {
        refresh.finishRefresh();
        loadMask.setNoNetworkText(msg);
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setReloadButtonText("点击重试~~");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("专题");
        MobclickAgent.onResume(this);
    }


    @Override
    protected void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("专题");
        MobclickAgent.onPause(this);
    }
}
